package  
{
	import org.flixel.*;
	/**
	 * ...
	 * @author morgan
	 */
	public class Bullet extends HitboxObject
	{
		[Embed (source = "../data/breakfast1_b.png")] private var img_bk1:Class;
		[Embed (source = "../data/breakfast2_b.png")] private var img_bk2:Class;
		[Embed (source = "../data/breakfast3_b.png")] private var img_bk3:Class;
		[Embed (source = "../data/lunch1_b.png")] private var img_ln1:Class;
		[Embed (source = "../data/lunch2_b.png")] private var img_ln2:Class;
		[Embed (source = "../data/lunch3_b.png")] private var img_ln3:Class;
		[Embed (source = "../data/dinner1_b.png")] private var img_dn1:Class;
		[Embed (source = "../data/dinner2_b.png")] private var img_dn2:Class;
		[Embed (source = "../data/dinner3_b.png")] private var img_dn3:Class;
		[Embed (source = "../data/rock1.png")] private var img_rk1:Class;
		[Embed (source = "../data/rock2.png")] private var img_rk2:Class;
		[Embed (source = "../data/rock3.png")] private var img_rk3:Class;		
		[Embed (source = "../data/bone.png")] private var img_bone:Class;
		[Embed (source = "../data/candy.png")] private var img_candy:Class;
		[Embed (source = "../data/missile.png")] private var img_missile:Class;
		
		public static var BREAKFAST:int = 0;
		public static var LUNCH:int = 1;
		public static var DINNER:int = 2;
		public static var PLAYER:int = 3;
		public static var ROTTEN:int = 4;
		public static var CANDY:int = 5;
		
		public var type:int = 0;
		public var life:Number = 0;
		public var age:Number = 0;
		public var pattern:BulletPattern;
		public var lastBehavior:BulletBehavior;
		
		public function Bullet() 
		{
			super(0, 0);
			exists = false;		
			
			HITBOX = 4;
		}
		
		public function spawn(type:int, x:int, y:int, vx:Number, vy:Number, pattern:BulletPattern, isMissile:Boolean):void
		{
			var rnd3:int = Math.random() * 3;
			this.type = type;
			this.pattern = pattern;
			_bakedRotation = 0;
			if (type == BREAKFAST)
			{
				if (rnd3 == 0) { loadRotatedGraphic(img_bk1, 4); }
				if (rnd3 == 1) { loadRotatedGraphic(img_bk2, 4); }
				if (rnd3 == 2) { loadRotatedGraphic(img_bk3, 4); }
				angle = Math.random() * 360;
				angularVelocity = 360;				
				
			}
			else if (type == LUNCH)
			{
				if (rnd3 == 0) { loadRotatedGraphic(img_ln1, 4); }
				if (rnd3 == 1) { loadRotatedGraphic(img_ln2, 4); }
				if (rnd3 == 2) { loadRotatedGraphic(img_ln3, 4); }
				angle = Math.random() * 360;
				angularVelocity = 360;				
				
			}	
			else if (type == DINNER)
			{
				if (rnd3 == 0) { loadRotatedGraphic(img_dn1, 4); }
				if (rnd3 == 1) { loadRotatedGraphic(img_dn2, 4); }
				if (rnd3 == 2) { loadRotatedGraphic(img_dn3, 4); }
				angle = Math.random() * 360;
				angularVelocity = 360;				
				
			}
			else if (type == ROTTEN)
			{
				if (rnd3 == 0) { loadRotatedGraphic(img_rk1, 4); }
				if (rnd3 == 1) { loadRotatedGraphic(img_rk2, 4); }
				if (rnd3 == 2) { loadRotatedGraphic(img_rk3, 4); }
				angle = Math.random() * 360;
				angularVelocity = 360;				
			}
			else if (type == CANDY)
			{
				loadGraphic(img_candy, true, false, 16, 16);
				addAnimation("default", [0, 1, 2, 3], 4, true);
				play("default");
				angularVelocity = 0;
				angle = 0;
			}			
			else
			{
				if (isMissile)
				{
					loadGraphic(img_missile, true, false, 7, 7);
					_bakedRotation = 0;
				}
				else
				{
					loadRotatedGraphic(img_bone, 4);
					angularVelocity = 360;
					angle = Math.random() * 360;
				}
			}
			exists = true;
			this.x = x;
			this.y = y;
			velocity.x = vx;
			velocity.y = vy;
			life = 10.0;
			age = 0;
			
			scale.x = 1; scale.y = 1;
			
			offset.x = width / 2 - HITBOX / 2;
			offset.y = height / 2 - HITBOX / 2;
			width = HITBOX; height = HITBOX;
			x -= HITBOX/2;
			y -= HITBOX / 2;
		}
		
		override public function update():void 
		{
			pattern.updateBehaviors(age - FlxG.elapsed, age, this);			
			
			life -= FlxG.elapsed;
			age += FlxG.elapsed;	
			
			if (life < 0) { exists = false; }
			
			drawHitbox -= FlxG.elapsed;
			
			super.update();
		}
		
	}

}